package com.ys.dubbo.consumer.config;


import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ConsumerConfig;
import org.apache.dubbo.config.MetadataReportConfig;
import org.apache.dubbo.config.MonitorConfig;
import org.apache.dubbo.config.ProtocolConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.Assert;

/**
 * <p>@description:消费端dubbo配置  用于测试 </p>
 *
 * @author yangshuang
 * @version 1.0
 * @date 2018/7/17 17:22
 */
@Configuration
public class DubboConsumerConfig {


    @Bean
    public RegistryConfig registry(@Value("${dubbo.registry.address}") String addr, @Value("${dubbo.zk.timeout}") Integer timeout) {
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress(addr);
        registryConfig.setTimeout(timeout);
        return registryConfig;
    }

    @Bean
    public ApplicationConfig application(@Value("${dubbo.application.name}") String appName, @Value("${dubbo.application.owner}") String appOwner) {
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName(appName);
        applicationConfig.setOwner(appOwner);
        return applicationConfig;
    }


    @Bean
    public MetadataReportConfig metadataReportConfig(@Value("${dubbo.registry.address}") String addr) {

        MetadataReportConfig metadataReportConfig = new MetadataReportConfig();
        metadataReportConfig.setAddress(addr);
        return metadataReportConfig;
    }

    @Bean
    public MonitorConfig monitorConfig(@Value("${dubbo.monitor.protocol}") String protocol) {
        MonitorConfig mc = new MonitorConfig();
        mc.setProtocol(protocol);
        return mc;
    }


    @Bean
    @ConditionalOnProperty("dubbo.protocol.port")
    public ProtocolConfig protocol(@Value("${dubbo.protocol.name:dubbo}") String name, @Value("${dubbo.protocol.port}") Integer port) {
        Assert.notNull(port, "dubbo.protocol.port  cannot be null");

        ProtocolConfig protocolConfig = new ProtocolConfig();
        protocolConfig.setName(name);
        protocolConfig.setPort(port);
        return protocolConfig;
    }

    @Bean
    public ConsumerConfig consumer(@Value("${dubbo.consumer.check}") boolean check) {
        ConsumerConfig consumerConfig = new ConsumerConfig();
        consumerConfig.setCheck(check);
        return consumerConfig;
    }


}
